package com.witmore.skyline.web.service.impl;

import com.witmore.skyline.web.controller.enums.WitmoreErrorCode;
import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import com.witmore.skyline.web.service.DeviceTimeService;
import com.witmore.skyline.web.service.third.yjx.client.ApiDeviceDataClient;
import com.witmore.skyline.web.service.third.yjx.dto.WorkDetailDTO;
import com.witmore.skyline.web.service.third.yjx.dto.WorkTimeListDTO;
import com.witmore.skyline.web.utils.LocalDateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * Description: DeviceServiceImpl
 * Author: 豆子高
 * Date: 2025/2/25 10:14
 */
@Slf4j
@Service
public class DeviceTimeServiceImpl implements DeviceTimeService {

    @Autowired
    private ApiDeviceDataClient apiDeviceDataClient;


    @Override
    public ResponseResult<List<WorkDetailDTO>> workTimeDetails(String deviceSn, LocalDate date) {
        if (StringUtils.isEmpty(deviceSn) || null == date) {
            return ResponseResult.fail(WitmoreErrorCode.PARAM_ERROR);
        }
        try {
            List<WorkDetailDTO> workDetailDTOS = apiDeviceDataClient.workTimeDetails(deviceSn, LocalDateUtils.dateConvertString(date));
            return ResponseResult.success(workDetailDTOS);
        } catch (Exception e) {
            log.error("设备某天工作详情获取失败 error msg-->{},deviceSn-->{},date-->{}, e",
                    e.getMessage(), deviceSn, date, e);
            return ResponseResult.fail(WitmoreErrorCode.SYSTEM_ERROR.getCode(),e.getMessage());
        }
    }

    @Override
    public ResponseResult<List<WorkDetailDTO>> workTimeDetailWithStatus(String deviceSn, LocalDate date) {
        if (StringUtils.isEmpty(deviceSn) || null == date) {
            return ResponseResult.fail(WitmoreErrorCode.PARAM_ERROR);
        }
        try {
            // 直接调用API客户端，缓存逻辑由AOP自动处理
            List<WorkDetailDTO> workDetailDTOS = apiDeviceDataClient.workTimeDetailWithStatus(deviceSn, LocalDateUtils.dateConvertString(date));
            return ResponseResult.success(workDetailDTOS);
        } catch (Exception e) {
            log.error("设备某天工作详情带状态获取失败 error msg-->{},deviceSn-->{},date-->{}, e",
                    e.getMessage(), deviceSn, date, e);
            return ResponseResult.fail(WitmoreErrorCode.SYSTEM_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public ResponseResult<List<WorkTimeListDTO>> workTimeList(String deviceSn, LocalDate startDay, LocalDate endDay) {
        if (StringUtils.isEmpty(deviceSn) || null == startDay || null == endDay) {
            return ResponseResult.fail(WitmoreErrorCode.PARAM_ERROR);
        }
        // 校验时间格式 最多三十天
        if (startDay.isAfter(endDay)) {
            return ResponseResult.fail(WitmoreErrorCode.START_DATE_AFTER_END_DATE);
        }
        if (ChronoUnit.DAYS.between(startDay, endDay) > 30) {
            return ResponseResult.fail(WitmoreErrorCode.DATE_INTERVAL_NOT_ALLOWED_30);
        }

        String startDayStr = LocalDateUtils.dateConvertString(startDay);
        String endDayStr = LocalDateUtils.dateConvertString(endDay);
        try {
            // 直接调用API客户端，缓存逻辑由AOP自动处理
            List<WorkTimeListDTO> workTimeListDTOS = apiDeviceDataClient.workTimeList(deviceSn, startDayStr, endDayStr);
            return ResponseResult.success(workTimeListDTOS);
        } catch (Exception e) {
            log.error("设备工时统计获取失败 error msg-->{},deviceSn-->{},startDay-->{},endDay-->{}, e",
                    e.getMessage(), deviceSn, startDay, endDay, e);
            return ResponseResult.fail(WitmoreErrorCode.SYSTEM_ERROR.getCode(),e.getMessage());
        }
    }


}
